package com.ybfenpan.controller.member.shop;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.ParamWage;
import com.ybfenpan.model.shop.Goods;
import com.ybfenpan.model.shop.Orders;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.ParamWageService;
import com.ybfenpan.service.shop.GoodsService;
import com.ybfenpan.service.shop.OrdersService;
import com.ybfenpan.taglib.pagetag.Pagination;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;





/**
 * Created by Administrator on 2016/3/25.
 */
@Controller

public class GoodsController extends BaseController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private MemberService memberService;
    @Resource
    private OrdersService ordersService;
    @Resource
    private ParamWageService paramWageService;
   
    
    @RequestMapping("member/shop/goodsList.shtml")
    public String list(HttpServletRequest request) throws Exception{
        HashMap formMap = getParameterMap(request);
        
        
        
        formMap.put("isActive", "T");	
        formMap.put("where", "1=1 and totalNum> 0 ");	
        formMap.put("pagination.orderBy","  id desc");  //排列
        Pagination page = goodsService.queryForPageList(formMap);
        
        
        request.setAttribute(Pagination.pageBeanName, page);
        return "member/shop/goodsList";
    }

    @RequestMapping("member/shop/goodsShow.shtml")
    public String show(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        if(0 < id){
        	Goods goods = goodsService.getByKey(id);
        	if(goods.getIsActive().equals("T")){
        		request.setAttribute("goods",goods);
        		request.setAttribute("id",id);
                
        	}  
    		
        }
        return "member/shop/goodsShow";
       
   } 
    
    @RequestMapping("member/shop/goodsBuy.shtml")
	public String pay(HttpServletRequest request, ModelMap model) throws Exception{
		Integer type = 0;
    	
		Boolean result=true;
		//代理商信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		
		Map formMap = getParameterMap(request);
		String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
		String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
		int id = MapUtil.getIntValue(formMap, "id");

		//转账类型
		int payWay = MapUtil.getIntValue(formMap, "payWay");

		String personName = MapUtil.getString(formMap, "personName");
		String address = MapUtil.getString(formMap, "address");
		String tel = MapUtil.getString(formMap, "tel");
		String code = MapUtil.getString(formMap, "code");
		String note = MapUtil.getString(formMap, "note");
		Integer num = MapUtil.getIntValue(formMap, "num");
		Goods goods = goodsService.getByKey(id);
		if(0 < id){

        	if(goods.getIsActive().equals("T")){
        		request.setAttribute("goods",goods);
                
                formMap.put("id", id);
                
                
                if(!member.getTwoPassword().equals(pwd)){
            		setMessage(model,"安全密码不正确。");
            		result=false;
            	}else{
            		 
            		//转账金额验证

            		if(num<0){
            			setMessage(model,"请输入正确的购买数量。");
                		result=false;
            		}else{
            			if(num>goods.getTotalNum()){
            				setMessage(model,"库存量不足。");
                    		result=false;
            			}else{
            				if(payWay==10){
            					type = 10;
            					//转账
	        					if(member.getMoney() < num * goods.getPrice()){
	        						setMessage(model,"现金积分余额不足");
	                        		result=false;
	        					}

            				} 
            
            			}
            		}
            	}
        	}  
    	}

		if (result){
			Random random = new Random();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
			String orderNo="B"+formatter.format(new Date()) + random.nextInt(2) ;


			Orders orders = new Orders();
			orders.setOrderNo(orderNo);
			orders.setGoodsPrice(goods.getPrice());
			orders.setGoodsTitle(goods.getTitle());
			orders.setBuyNum(num);
			orders.setPayPrice(num*goods.getPrice());
			orders.setPayWay(10);
			orders.setPersonName(personName);
			orders.setAddress(address);
			orders.setTel(tel);
			orders.setCode(code);
			orders.setNote(note);
			orders.setMemberId(member.getId());
			this.ordersService.saveweiyi(member,orders,payWay,num*goods.getPrice(),orderNo,id,type,num);
			return "redirect:ordersList.shtml";
		}else {
			return "member/shop/goodsShow";
		}

	}
    
    
    
}
